package com.pactera.madp.cp.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pactera.madp.cp.api.dto.product.ProductEmployeeAddDto;
import com.pactera.madp.cp.api.dto.product.ProductEmployeeUpdateDto;
import com.pactera.madp.cp.api.em.product.ProductPhotoEnum;
import com.pactera.madp.cp.api.entity.product.ProdEmployeeEntity;
import com.pactera.madp.cp.api.entity.product.ProdEmployeeGroupEntity;
import com.pactera.madp.cp.api.entity.product.ProductGroupEntity;
import com.pactera.madp.cp.api.vo.product.ProductEmployeeVO;
import com.pactera.madp.cp.handler.CorpContextHolder;
import com.pactera.madp.cp.mapper.ProdEmployeeMapper;
import com.pactera.madp.cp.service.ProdEmployeeGroupService;
import com.pactera.madp.cp.service.ProdEmployeeService;
import com.pactera.madp.cp.service.ProductGroupService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 产品经理表(CpProdEmployee)表服务实现类
 *
 * @author yushen.qi
 * @since 2021-12-17 16:19:54
 */
@Service("cpProdEmployeeService")
public class ProdEmployeeServiceImpl extends ServiceImpl<ProdEmployeeMapper, ProdEmployeeEntity> implements ProdEmployeeService {

    @Resource
    private ProdEmployeeGroupService prodEmployeeGroupService;
    @Resource
    private ProductGroupService productGroupService;

    @Override
    public boolean save(ProductEmployeeAddDto dto) {
        Integer corpId = CorpContextHolder.getCorpId();
        ProdEmployeeEntity prodEmployeeEntity = new ProdEmployeeEntity();
        BeanUtil.copyProperties(dto,prodEmployeeEntity);
        prodEmployeeEntity.setCorpId(corpId);
        prodEmployeeEntity.setCreateTime(LocalDateTime.now());
        prodEmployeeEntity.setUpdateTime(LocalDateTime.now());
        prodEmployeeEntity.setBackImg("/mfs/logo.png");
        prodEmployeeEntity.setBannerImg("/mfs/remind.png");
        ProdEmployeeGroupEntity prodEmployeeGroupEntity = new ProdEmployeeGroupEntity();
        String[] range = dto.getProdRange().split(",");
        for (String s : range) {
            int i = Integer.parseInt(s);
            ProductGroupEntity one = productGroupService.getOne(Wrappers.<ProductGroupEntity>lambdaQuery().eq(ProductGroupEntity::getId, i));
            if(one!=null){
                prodEmployeeGroupEntity.setProdGroupName(one.getName());
            }
            prodEmployeeGroupEntity.setEmployeeId(dto.getEmployeeId());
            prodEmployeeGroupEntity.setProdGroupId(i);
            prodEmployeeGroupEntity.setCorpId(corpId);
            prodEmployeeGroupEntity.setProdGroupImg(ProductPhotoEnum.getTypeByCode(Integer.parseInt(s)));
            prodEmployeeGroupEntity.setCreateTime(LocalDateTime.now());
            prodEmployeeGroupService.save(prodEmployeeGroupEntity);

        }
        return save(prodEmployeeEntity);
    }

    @Override
    public boolean put(ProductEmployeeUpdateDto dto) {
        ProdEmployeeEntity prodEmployeeEntity = new ProdEmployeeEntity();
        BeanUtil.copyProperties(dto,prodEmployeeEntity);
        prodEmployeeEntity.setUpdateTime(LocalDateTime.now());
        return update(prodEmployeeEntity, Wrappers.<ProdEmployeeEntity>lambdaQuery().eq(ProdEmployeeEntity::getId,dto.getId()));
    }

    @Override
    public  Page<ProductEmployeeVO> getPage(Page page, String name) {
        Page<ProdEmployeeEntity> req = new Page<>();
        req.setSize(page.getSize());
        req.setCurrent(page.getCurrent());
        IPage<ProdEmployeeEntity> page1 = page(req, Wrappers.<ProdEmployeeEntity>lambdaQuery()
                .eq(StrUtil.isNotBlank(name), ProdEmployeeEntity::getEmployeeName, name)
                .eq(ProdEmployeeEntity::getCorpId, CorpContextHolder.getCorpId())
        );
        List<ProdEmployeeEntity> records = page1.getRecords();
        List<ProductEmployeeVO>  vo= records.stream().map(entity -> {
            ProductEmployeeVO productEmployeeVO = new ProductEmployeeVO();
            if(StrUtil.isNotBlank(entity.getProdRange())){
                String[] split = entity.getProdRange().split(",");
                ArrayList<String> strings = new ArrayList<>(Arrays.asList(split));
                productEmployeeVO.setRange(strings);
            }
            BeanUtil.copyProperties(entity, productEmployeeVO);
            productEmployeeVO.setUserLevel(entity.getUserLevel());
            return productEmployeeVO;
        }).collect(Collectors.toList());
            page.setRecords(vo);
            page.setTotal(page1.getTotal());
        return page;
    }
}
